<template>
  <div style="overflow: auto;">
    <div class="page-body">
      <el-divider content-position="left">基本配置</el-divider>
      <el-form ref="formRef" label-width="80px" :model="formData" :rules="rules" label-position="left" size="small">
        <div style="display: flex;">
          <div style="flex-shrink: 0;flex-basis: 250px;">
            <el-form-item label="头像">
              <upload-img :doc-type="docType" v-model="formData._profile">
              </upload-img>
            </el-form-item>
          </div>
          <div style="flex-grow: 1;">
            <el-form-item label="编号">
              <el-input v-model="formData.Id" placeholder="自动生成" disabled></el-input>
            </el-form-item>

            <el-form-item label="名称" prop="Name">
              <el-input v-model="formData.Name"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="formData.Phone"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-input v-model="formData.Sex"></el-input>
            </el-form-item>
          </div>
        </div>

        <el-divider content-position="left">绑定门店</el-divider>
        <div class="page-mendian" v-loading="cmpLoading">
          <div v-for=" item  in  cmpList.concat(cmpList, cmpList, cmpList) ">
          </div>
        </div>
      </el-form>
    </div>

  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { queryGet } from "@/utils/api";
export default defineComponent({
  name: "BizUserInfo",
})
</script>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import ModalView from "@/components/ModalView";
import { appAction } from "@/store/app";
import { docType, BizUserDataClass } from "@/model/BizUser";
import { request } from "@/utils/axios";
import { UploadImg } from "@/components/UploadFile";
import { CmpDataClass } from "@/model/Cmp";
import CmpCard from "@/views/pages/Cmp/CmpCard.vue"

let props = withDefaults(defineProps<{
  appData?: BizUserDataClass
}>(), {
})


let emit = defineEmits<{
  (e: "close"): void;
  (e: "submit"): void
}>()

/**
 * 表单
 */
let formRef = ref();

/**
 * 表单
 */
let formData = ref<BizUserDataClass>(new BizUserDataClass({}));


/**
 * 表单验证
 */
let rules = reactive({
  Name: [{ required: true, message: '当前项为必填项', trigger: 'blur' }],
})

/**
 * 页面展示
 */
function show(data?: any) {
  formData.value = new BizUserDataClass(data || {});

  if (formData.value.Id) {
    //获取对应的门店
    getMenDian();
  }
}

/**
 *  保存前校验
 */
function beforeSubmit(): Promise<void> {
  return new Promise((resolve, reject) => {
    formRef.value.validate()
      .then(() => {

        let url = "";
        if (formData.value.Id) {
          //存在为修改
          url = "/" + docType + "/Update";

        } else {
          //不存在ID 为新增
          url = "/" + docType + "/Add";
        }

        //数据提交
        request(url, formData.value)
          .then(() => {
            ElMessage.success("表单数据保存成功");
            emit("submit");
            resolve();
          })
          .catch(() => {
            appAction.resetPageuuid();
            reject();
          })
      })
      .catch(reject)
  })
}

/**
 * 关闭页面
 */
function close() {
  emit("close")
}



/**
 * ====================
 *  门店信息
 * ====================
 */

/**
 * 门店列表
 */
let cmpList = ref<CmpDataClass[]>([]);

let cmpLoading = ref(false);

/**
 * 获取门店信息
 */

function getMenDian() {
  cmpLoading.value = true;
  queryGet({
    docType: "Cmp",
    actionType: "UserId",
    apiType: "SingleList"
  }, {
    Id: formData.value.Id
  })
    .then(res => {
      cmpList.value = res.map((item: any) => new CmpDataClass(item))
    })
    .finally(() => {
      cmpLoading.value = false;
    })
}


/**
 * ====================
 *  页面初始化
 * ====================
 */

onMounted(() => {
  show(props.appData)
})

/**
 * 导出
 */
defineExpose({
  show
})
</script>

<style lang="less" scoped>
.page-body {
  padding: 20px;
}

.page-mendian {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;

  &>div {
    flex-basis: calc(50% - 8px);
    margin-bottom: 15px;
  }
}
</style>